<template>
	<view class="content">
		<image class="logo" src="../../static/logo.png" mode="aspectFill"></image>
	</view>
</template>

<script setup>
	import {
		getUserOpenId
	} from "../../utils/getOpenId";
	import {
		md5
	} from 'js-md5';

	import {
		getUserScoreFromCloud,
		saveUserCloudScoreToLocal
	} from '../../store/scoreManager.js'
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app"
	import {
		ref
	} from "vue";
	const usertype = ref(0)
	const db = uniCloud.database();
	onLoad(e => {
		usertype.value = uni.getStorageSync("userType");
		console.log("usertype:", usertype.value);
		if (usertype.value == 1) {
			loginUser();
		} else {
			let appVersion = "2.0.7";
			db.collection("openKeys").where({
				version: appVersion
			}).get().then(res => {
				console.log("openKeys:", JSON.stringify(res.result.data));
				if (res.result.data && res.result.data.length > 0) {
					uni.setStorageSync("userType", 0);
					usertype.value = 0;
					uni.reLaunch({
						url: '/pages/index/shouye' + '?usertype=' + usertype.value
					})
				} else {
					uni.setStorageSync("userType", 1);
					usertype.value = 1;
					loginUser();
				}
			}).catch(e => {
				console.log("openKey:", e);
				uni.setStorageSync("userType", 0);
				usertype.value = 0;
				uni.reLaunch({
					url: '/pages/index/shouye' + '?usertype=' + usertype.value
				})
			})
		}
	})
	
	async function  loginUser() {
		let loginInfo = {};
		
		try {
			const openid = await getUserOpenId();
			const password = md5(openid).substring(0, 10);
			const username = openid.toLowerCase();
			const res = db.collection('open-user').where({username:username}).get();
				console.log("loginerror:", JSON.stringify(res));
			if(res.result.data &&res.result.data.length>0){
				uni.reLaunch({
					url: '/pages/index/shouye' + '?usertype=' + this.usertype
				})
			}else{
				uni.reLaunch({
					url: '/pages/register/register'
				})
			}
		} catch (e) { //登录失败
			console.log("loginerror:", e);
			
		}
	}
</script>

// <!-- // scss 语言类型  scoped css样式的类名仅限当前页面 不污染其它页面 -->
<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.text-area {
		display: flex;
		justify-content: center;
		color: gold;
		font-size: 100rpx;
		font-weight: bold;
		width: 100%;
		text-align: center;
	}

	.logo {
		height: 250rpx;
		width: 250rpx;
		margin-top: 30vh;
		border-radius: 50%;
	}
</style>